package com.qf.forumproject.blog.controller;

import cn.hutool.core.date.DateUtil;
import com.qf.forumproject.entity.QfUser;
import com.qf.forumproject.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.qf.forumproject.entity.QfBlog;
import com.qf.forumproject.blog.service.QfBlogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (QfBlog)控制层
 *
 * @author protagonist
 * @since 2022-07-23 11:37:12
 */
@RestController
@Slf4j
@RequestMapping("/qfBlog")
@Api(tags = "博客文章接口")

public class QfBlogController {
    /**
     * 服务对象
     */
    @Resource
    private QfBlogService qfBlogServiceImpl;


    /**
     * @param
     * @return
     */
    @GetMapping(value = "/get")
    @ApiOperation("模糊查询接口")
    public Result selectByPage(SearchVo blogSearchVo) {

        //初始化分页参数
        //PageInit.initPage(blogSearchVo);

        List<QfBlog> result = qfBlogServiceImpl.selectByPage(blogSearchVo);
        if(Objects.nonNull(result)){
            return new Result<>(true,StatusCode.OK,"查询成功",result);
        }
        return new Result<>(false,StatusCode.ERROR,"查询失败");
    }

    @GetMapping(value = "/getAll")
    @ApiOperation("查询所有文章接口")
    public Result selectAll(){
        //初始化分页参数
        //PageInit.initPage(blogSearchVo);

        List<ResultVo> blogs = qfBlogServiceImpl.selectAllByPage();
        if(Objects.nonNull(blogs)){
            return new Result<>(true,StatusCode.OK,"查询成功",blogs);
        }
        return new Result<>(false,StatusCode.ERROR,"查询失败");
    }

    
    /**
     * 发布文章接口
     *
     * @param qfBlog 映射类
     * @return Result对象
     */
    @GetMapping(value = "/insert")
    @ApiOperation("发布文章接口")
    public Result insert(QfBlog qfBlog) {

        Subject subject = SecurityUtils.getSubject();
        QfUser user = (QfUser) subject.getPrincipal();

        qfBlog.setAuthorId(user.getId());

        QfBlog blog = qfBlogServiceImpl.addQfBlog(qfBlog);
        if (blog != null) {
            return new Result<>(true,StatusCode.OK,"博客文章新增成功",blog);
        }
        return new Result<>(false,StatusCode.ERROR,"博客文章新增失败");
    }

    /**
     * 更新文章浏览量
     * @return
     */
    @GetMapping(value = "/clickPassage")
    @ApiOperation("更新文章浏览量")
    public Result updateViews(Integer id){


        QfBlog blog = qfBlogServiceImpl.updateViews(id);

        if (blog != null) {
            return new Result<>(true,StatusCode.OK,"文章浏览量新增成功",blog);
        }
        return new Result<>(false,StatusCode.ERROR,"文章浏览量新增失败");
    }


    /**
     * 根据id查询文章及所有评论
     * @param id
     * @return
     */
    @GetMapping("/getBlogAndComment")
    @ApiOperation("查询一条文章所有评论")
    public Result getBlogAndComment(Integer id){

        List<BlogAndCommentVo> blogAndCommentVos = qfBlogServiceImpl.selectBlogAndCommentVos(id);
        if (blogAndCommentVos != null) {
            return new Result<>(true,StatusCode.OK,"文章和评论查询成功",blogAndCommentVos);
        }
        return new Result<>(false,StatusCode.ERROR,"文章和评论查询失败");
    }


    /**
     * 查询文章总条数
     * @return
     */
    @GetMapping("/getBlogCount")
    @ApiOperation("查询文章总数")
    public Result getBlogCount(){
        long count = qfBlogServiceImpl.count();
        if (count > 0) {
            return new Result<>(true,StatusCode.OK,"文章总条数查询成功",count);
        }
        return new Result<>(false,StatusCode.ERROR,"文章总条数查询失败");
    }

}
